package com.texas_android.app.http;

import android.content.Context;

import com.google.gson.reflect.TypeToken;
import com.loopj.android.http.TextHttpResponseHandler;
import com.texas_android.app.utils.AppOperator;
import com.texas_android.app.utils.Logger;
import com.texas_android.app.utils.bean.BaseBean;
import com.universal_library.weight.AppToast;

import cz.msebera.android.httpclient.Header;

/**
 * Created by solin on 2017/5/2.
 */

public class ResponseHandler extends TextHttpResponseHandler {
    private Context mContext;
    private boolean mshowToast;
    private String Tag = "";

    public ResponseHandler(Context mContext, boolean showToast) {
        this.mContext = mContext;
        this.mshowToast = showToast;
    }

    public ResponseHandler(Context mContext, boolean showToast, String tag) {
        this.mContext = mContext;
        this.mshowToast = showToast;
        this.Tag = tag;
    }

    public BaseBean baseBean = null;

    @Override
    public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
        Logger.log(Tag + ":" + responseString);
        if (statusCode != 200) {
//            AppToast.showToast(mContext, "", "服务器走神了");
        }
    }

    @Override
    public void onSuccess(int statusCode, Header[] headers, String responseString) {
        Logger.log(Tag + ":" + responseString);
        baseBean = AppOperator.createGson().fromJson(responseString, new TypeToken<BaseBean>() {
        }.getType());
        if (!"0".equals(baseBean.getStatus()) && mshowToast) {
            AppToast.showToast(mContext, "", baseBean.getStatusDesc());
            return;
        }
    }
}
